import Taro, { Component } from '@tarojs/taro'
import { View, Text, Image } from '@tarojs/components'
import { DNavBar, Ddialog } from '@/components'
import sdk from '@/common/sdk'
import http from '@/api'
import './usermoney.scss'

const bgImages = require('../assets/bgImages.png')
export default class UserMoneyBox extends Component {
    constructor(props) {
        super(props)
        this.state = {
            accountDialogCon: '企业账户未开通电子收款账户，将会影响企业收款，是否前往开通？',
            companyintro: {},
            userInfo: {},
            eaIsAppro: '',
            title: '我的收益',
            list: [
                {
                    img: require('../assets/company.png'),
                    text: '企业订单'
                },
                {
                    img: require('../assets/bank.png'),
                    // text: '我的银行卡'
                    text: '账户信息'
                },
                {
                    img: require('../assets/money.png'),
                    text: '提现记录'
                },
                // {
                //     img: require('../assets/product.png'),
                //     text: '理财产品'
                // }
            ],
            isShow: false,
            showAccDialog: false,
        }
    }

    componentWillMount() {
        this.getInfo()

    }
    async getInfo() {
        const userInfo = await http.myselfDetail()
        const { eaIsAppro } = await http.getConfirmInfo()

        this.setState({
            userInfo,
            eaIsAppro
        }, async () => {
            if (await this.infoByEnterprise()) {
                this.filterTotalMoney()
            }
        })
    }

    async filterTotalMoney() {
        /** amtType: 类型       amt: 余额
         * 01：可用余额
         * 02：实际余额
         * 03：冻结余额
         * 04：待结算余额
         * 10：可用不可出金余额
         * 11：不可用可出金余额
         */

        const { LISTS: list } = await http.getBalance()
        // temp1 可用余额    temp2 可用不可出金余额    temp3 不可用可出金余额
        let temp1, temp2, temp3, totalMoney

        list.forEach(item => {
            // item.amtType == '01' && (temp1 = item.amt)
            // item.amtType == '10' && (temp2 = item.amt)
            // item.amtType == '11' && (temp3 = item.amt)
            item.amtType == '02' && (totalMoney = item.amt)
        })
        // 可出金余额 = 可用余额 - 可用不可出金余额 + 不可用可出金余额
        this.setState({
            // withdrawBalance: (temp1 - temp2 + +temp3) / 100
            withdrawBalance: totalMoney / 100
        })
    }
    componentDidShow() {
        sdk.setTitleInfo(this.state.title)
    }

    async applyHandle() {
        if (await this.infoByEnterprise()) {
            Taro.navigateTo({
                url: "/pages/applymoney/applymoney"
            })
        }
    }
    async infoByEnterprise() {
        // infoByEnterprise 这个接口如果返回空对象则说明是没有开通电子登记簿，需要引导用户开通电子登记簿
        let accountInfo = await http.infoByEnterprise()
        let keys = Object.keys(accountInfo)
        let { userInfo, accountDialogCon, eaIsAppro } = this.state

        if (eaIsAppro != 1 && userInfo.imIdStatus != 2) { // 没有信息认证
            return this.setState({
                isShow: true
            })
        }

        if (!keys.length) {
            if (userInfo.imIdStatus == 2) {
                accountDialogCon = "本企业尚未开通企业收款账户，请联系企业管理员开通后再操作。"
            }
            this.setState({
                showAccDialog: true,
                accountDialogCon
            })
        } else {
            if (accountInfo.status === '') {
                return this.setState({
                    showAccDialog: true,
                    accountDialogCon: '电子登记簿开通失败，请重新开通'
                })
            }
            // 已经开通电子登记簿
            return true
        }
    }
    async onActiveHandle(i) {
        let objUrl = {
            0: '/pages/businessorder/businessorder',
            // 1: '/pages/userbank/userbank',
            1: '/pages/payaccount',
            2: '/pages/moneyrecord/moneyrecord',
            3: '/pages/moneyproduct/moneyproduct'
        }
        if (!objUrl[i]) {
            return
        }
        if (i == 1 || i == 2) {
            // 校验是否开通点击登记簿
            if (await this.infoByEnterprise()) {
                Taro.navigateTo({
                    url: objUrl[i]
                })
            }
        } else {
            Taro.navigateTo({
                url: objUrl[i]
            })
        }
    }
    onCancel() {
        this.setState({
            isShow: false,
            showAccDialog: false
        })
    }
    onSure(type) {
        this.onCancel()
        if (type == 'openAcc') {
            if (this.state.userInfo.imIdStatus == 2) return
            Taro.navigateTo({
                url: `/pages/accountBook/accountBook`
            })
        } else {
            Taro.navigateTo({
                url: `/pages/myinfo/authentication`
            })
        }
    }

    render() {
        const { title, list, isShow, showAccDialog, withdrawBalance, userInfo, accountDialogCon } = this.state
        return (
            <View className="b_usermoney">
                <Image src={bgImages} className="b_usermoney__images"></Image>
                <DNavBar title={title} className="b_usermoney__nav"></DNavBar>
                <View className="b_usermoney__box">
                    <View className="b_usermoney__header">
                        <View className="b_usermoney__header_left">
                            <View className="b_usermoney__header_left__title">收入总金额</View>
                            <View className="b_usermoney__header_left__money">
                                <Text className="item_money__text">{withdrawBalance || '00.00'}元</Text>
                            </View>
                            <View className="b_usermoney__header_left__total">
                                <Text className="item_total__secode">总结金额：{withdrawBalance || '00.00'}元</Text>
                            </View>
                        </View>
                        {
                            userInfo.imIdStatus == 1 && <View className="b_usermoney__header_right" onClick={this.applyHandle.bind(this)}>提现</View>
                        }
                    </View>
                    <View className="b_usermoney__content">
                        <View className="b_usermoney__content_title">收益服务</View>
                        <View className="b_usermoney__content_box">
                            {list &&
                                list.map((item, i) => {
                                    return (
                                        <View
                                            className="b_usermoney__list"
                                            key={i}
                                            onClick={this.onActiveHandle.bind(this, i)}
                                        >
                                            <Image src={item.img} className="b_usermoney__list_img"></Image>
                                            <View className="b_usermoney__list_text">{item.text}</View>
                                        </View>
                                    )
                                })}
                        </View>
                    </View>
                </View>

                <Ddialog isShow={isShow} content="为了您的交易安全，请进行信息认证 信息认证后可或得更多权限" btnText="信息认证" onCancel={this.onCancel.bind(this)} onSure={this.onSure.bind(this)}></Ddialog>
                <Ddialog isShow={showAccDialog} content={accountDialogCon} btnText={userInfo.imIdStatus == 1 ? '前往开通' : '我知道了'} onCancel={this.onCancel.bind(this)} onSure={this.onSure.bind(this, "openAcc")}></Ddialog>
            </View>
        )
    }
}
